﻿using MongoDB.Bson;
using MongoDB.Driver.Builders;

namespace nMongo
{
    public partial class MongoRepository<T>
    {        
        /// <summary>
        /// Retrieves an object of the current collection, using the identity field.
        /// </summary>
        /// <param name="id">Value of the identity field.</param>
        public virtual T GetById(object id)
        {
            T returned = OriginalCollection.FindOne(Query.EQ("_id", BsonValue.Create(id)));

            if (returned != null)
            {
                this.AutoDetectPersistencyById(MongoPersistencyState.Retrieved, id);
            }
            return returned;
        }
    }
}
